package com.dld.android.net;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.util.zip.GZIPInputStream;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NoHttpResponseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;

import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;

import com.dld.android.util.Device;
import com.dld.android.util.LogUtils;

public class HttpUser extends Connector {
	private Callback callback;
	private boolean cancel;
	private HttpConfig config;
	private HttpURLConnection conn;
	private Context context;
	private InputStream in;
	private OutputStream outputstream;
	private Param params;

	public HttpUser(Callback paramCallback, HttpConfig paramHttpConfig,
			Param paramParam, Context paramContext) {
		this.callback = paramCallback;
		this.config = paramHttpConfig;
		this.params = paramParam;
		this.context = paramContext;
	}

	public static HttpClient getHttpClient(Context paramContext) {
		Object localObject1 = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout((HttpParams) localObject1,
				10000);
		HttpConnectionParams.setSoTimeout((HttpParams) localObject1, 60000);
		localObject1 = new DefaultHttpClient((HttpParams) localObject1);
		Object localObject2 = new DefaultHttpRequestRetryHandler();
		NoRetryNoResponseRetryHandler localNoRetryNoResponseRetryHandler = new NoRetryNoResponseRetryHandler();
		localNoRetryNoResponseRetryHandler
				.setHandler((HttpRequestRetryHandler) localObject2);
		((DefaultHttpClient) localObject1)
				.setHttpRequestRetryHandler(localNoRetryNoResponseRetryHandler);
		localObject2 = ((ConnectivityManager) paramContext
				.getSystemService("connectivity")).getNetworkInfo(1);
		LogUtils.log("main", "wifi is connected="
				+ ((NetworkInfo) localObject2).isConnected());
		if (!((NetworkInfo) localObject2).isConnected()) {
			localObject2 = Uri.parse("content://telephony/carriers/preferapn");
			localObject2 = paramContext.getContentResolver().query(
					(Uri) localObject2, null, null, null, null);
			if ((localObject2 != null)
					&& (((Cursor) localObject2).moveToFirst())) {
				localObject2 = ((Cursor) localObject2)
						.getString(((Cursor) localObject2)
								.getColumnIndex("proxy"));
				if ((localObject2 != null)
						&& (((String) localObject2).trim().length() > 0)) {
					localObject2 = new HttpHost((String) localObject2, 80);
					((DefaultHttpClient) localObject1).getParams()
							.setParameter("http.route.default-proxy",
									localObject2);
				}
			}
		}
		return (HttpClient) (HttpClient) localObject1;
	}

	public static byte[] read(HttpResponse paramHttpResponse) {
		Object localObject = paramHttpResponse.getEntity();
		ByteArrayOutputStream localByteArrayOutputStream = null;
		try {
			localObject = ((HttpEntity) localObject).getContent();
			localByteArrayOutputStream = new ByteArrayOutputStream();
			while (true) {
				try {
					Header localHeader = paramHttpResponse
							.getFirstHeader("Content-Encoding");
					if ((localHeader == null)
							|| (localHeader.getValue().toLowerCase().indexOf(
									"gzip") <= -1))
						continue;
					localObject = new GZIPInputStream((InputStream) localObject);
					byte[] arrayOfByte = new byte[512];
					int i = ((InputStream) localObject).read(arrayOfByte);
					if (i != -1)
						continue;
					localByteArrayOutputStream = localByteArrayOutputStream;
					localByteArrayOutputStream.write(arrayOfByte, 0, i);
					continue;
				} catch (Exception localException1) {
					localByteArrayOutputStream = localByteArrayOutputStream;
				}
			}
		} catch (Exception localException2) {
			localException2.printStackTrace();
		}
		return localByteArrayOutputStream.toByteArray();
	}

	private void requestByGet(String paramString1, String paramString2)
			throws Exception {

	}

	// ERROR //
	private void requestByPost(String paramString, byte[] paramArrayOfByte)
			throws Exception {
	}

	public void close() {
		try {
			if (this.outputstream != null) {
				this.outputstream.close();
				this.outputstream = null;
			}
			if (this.in != null) {
				this.in.close();
				this.in = null;
			}
			if (this.conn != null) {
				this.conn.disconnect();
				this.conn = null;
			}
			return;
		} catch (IOException localIOException) {
			while (true)
				localIOException.printStackTrace();
		}
	}

	public void run() {
		int i = 0 + 1;
		try {
			String str1 = Device.getInstance().getNetType(
					this.callback.getContext());
			if ((str1 == null) || (str1.equals("")))
				this.callback.onException(new IOException(
						"No available net service!"));
			// return;
			str1 = this.callback.getUrl();
			LogUtils.log("Http", "完整的uri" + str1);
			LogUtils.log("main", "完整的url" + str1);

			this.callback.onException(new NullPointerException("Uri is null"));
			close();

			int j = str1.indexOf("?");
			if (j == -1) {
				// str2 = str1;
				label160: this.conn = null;
				if (!this.callback.custom)
					;
				// break label229;
			}
			label229: for (str1 = "GET";; str1 = this.config.getRequestMethod()) {
				LogUtils.log("main", "method==" + str1);
				boolean bool = this.cancel;
				if (!bool)
					return;
				// break label240;
				close();
				break;
				// str2 = str1.substring(0, str2);
				// break label160;
			}
			label240: LogUtils.log("Http", "<Url: > " + null);
			LogUtils.log("main", str1 + "///");
			if (str1.equalsIgnoreCase("POST")) {
				if (this.params != null)
					// requestByPost(str2,
					// this.params.toString().getBytes("UTF-8"));
					while (true) {
						// LogUtils.log("main", "请求的url" + str2);
						close();
						break;
						// requestByPost(str2, null);
					}
			}
		} catch (Exception localException) {
			// while (true)
			{
				localException.printStackTrace();
				close();
			}
		} finally {
			close();
		}
		// throw localObject;
	}

	private static class NoRetryNoResponseRetryHandler implements
			HttpRequestRetryHandler {
		private HttpRequestRetryHandler handler;

		public boolean retryRequest(IOException paramIOException, int paramInt,
				HttpContext paramHttpContext) {
			boolean bool;
			if ((paramIOException == null)
					|| (paramIOException.getClass() != NoHttpResponseException.class))
				bool = this.handler.retryRequest(paramIOException, paramInt,
						paramHttpContext);
			else
				bool = false;
			return bool;
		}

		public void setHandler(
				HttpRequestRetryHandler paramHttpRequestRetryHandler) {
			this.handler = paramHttpRequestRetryHandler;
		}
	}
}
